import {
  HTTP
} from '../util/http.js'
class classicModel extends HTTP {
  getLatest(sCallback) {
    this.request({
      url: 'classic/latest',
      success: res => {
        this._setLatestIndex(res.index)
        sCallback(res)
        let key = this._getkey(res.index);
        wx.setStorageSync(key,res)
      }
    })
  }
  // 点击上一页和下一页
  getClassic(index, nextOrPrev, sCallback) {
    let key = nextOrPrev == 'next'?this._getkey(index+1):this._getkey(index - 1);
    let classic = wx.getStorageSync(key)
    if(!classic){
      this.request({
        url: `classic/${index}/${nextOrPrev}`,
        success: (res) => {
          wx.setStorageSync(this._getkey(res.index),res)
          sCallback(res)
        }
      })
    }else{
      sCallback(classic)
    }
  }
  getMyFavor(success){
    const params = {
      url:'classic/favor',
      success:success
    }
    this.request(params)
  }

  isFirst(index) {
    return index == 1 ? true : false
  }

  isLatest(index) {
    let latestIndex = this._getLatestIndex();
    return latestIndex == index ? true : false
  };
  // 将当前一期的号码获取
  _setLatestIndex(index) {
    wx.setStorageSync('latest', index)
  }
  _getLatestIndex() {
    var index = wx.getStorageSync('latest')
    return index;
  }

  // 缓存用的key
  _getkey(index){
    let key = 'classic-' + index;
    return key;
  }
};

export {
  classicModel
};